//
//  YWGesturesUnLockView.m
//  GestureLock
//
//  Created by YR on 2017/6/5.
//  Copyright © 2017年 YR. All rights reserved.
//

#import "YWGesturesUnLockView.h"

#import "Masonry.h"

@interface YWGesturesUnLockView ()

@property (nonatomic, strong) NSMutableArray *selectedBtns;
@property (nonatomic, assign) CGPoint currentPoint;

@end

@implementation YWGesturesUnLockView
- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _selectedBtns = [NSMutableArray array];
    
        [self setup];
        [self layoutIfNeeded];
    }
    return self;
}

- (void)layoutIfNeeded {
    [super layoutIfNeeded];
    
    NSUInteger count = self.subviews.count;
    NSInteger cols = 3;  // 总列数
    
    CGFloat x = 0,y=0,w=0,h=0;
    if (Screen_Width == 320) {
        w = 50;
        h = 50;
    } else {
        w = 58;
        h = 58;
    }
    
    // 间距
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width - cols * w) / (cols + 1);
    
    CGFloat col = 0;
    CGFloat row = 0;
    for (int i = 0; i < count; i++) {
        col = i%cols;
        row = i/cols;
        
        x = margin + (w + margin) *col;
        y = margin + (w + margin) *row;
        
        if (Screen_Height == 480) {
            y = (w+margin)*row;
        } else {
            y = margin + (w+margin)*row;
        }
        
        UIButton *btn = self.subviews[i];
        btn.frame = CGRectMake(x, y, w, h);
    }
    
}

- (void)drawRect:(CGRect)rect {
    if (self.selectedBtns.count == 0) return;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    NSUInteger count = self.selectedBtns.count;
    for (int i = 0; i<count; i++) {
        UIButton *btn = self.selectedBtns[i];
        if (i==0) {
            // 设置起点
            [path moveToPoint:btn.center];
        } else {
            [path addLineToPoint:btn.center];
        }
    }
    [path addLineToPoint:_currentPoint];
    
    [UIColorFromRGB(0x7f67f0) set];
    path.lineWidth = 3;
    path.lineJoinStyle = kCGLineJoinRound;
    [path stroke];
}

- (void)setup {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self addGestureRecognizer:pan];
    
    //  创建9个按钮
    for (int i =0; i<9; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.userInteractionEnabled = NO;
        [btn setImage:[UIImage imageNamed:@"gesture_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"GesSelected_"] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"background"] forState:UIControlStateSelected];
        btn.tag = 1000+i;
        [self addSubview:btn];
    }
}

- (void)panAction:(UIPanGestureRecognizer *)pan {
    _currentPoint = [pan locationInView:self];
    
    [self setNeedsDisplay];
    
    for (UIButton *button in self.subviews) {
        if (CGRectContainsPoint(button.frame, _currentPoint) && button.selected == NO) {
            button.selected = YES;
            [self.selectedBtns addObject:button];
        }
    }
    
    [self layoutIfNeeded];
    
    //  拖动结束 将密码保存
    NSMutableString *geaturPswd = @"".mutableCopy;
    if (pan.state == UIGestureRecognizerStateEnded) {
        for (UIButton *button in self.selectedBtns) {
            [geaturPswd appendFormat:@"%ld",button.tag-1000];
            button.selected = NO;
        }
        [self.selectedBtns removeAllObjects];
        
        if ([self.delegate respondsToSelector:@selector(gesturesUnlockView:drawRectFinished:)]) {
            [self.delegate gesturesUnlockView:self drawRectFinished:geaturPswd];
        }
    }
}

@end
